#include "WorkerManager.h"

#include "UnitArbitrator.h"
#include "BaseManager.h"
#include "..\Util.h"

namespace BroodWar
{
	namespace Addon
	{
		WorkerManager::WorkerManager(BWSAL::WorkerManager* manager)
		{
			instance = manager;
		}

		WorkerManager::~WorkerManager()
		{
			BWSAL::WorkerManager::destroy();
		}

		WorkerManager::!WorkerManager()
		{
			BWSAL::WorkerManager::destroy();
		}

		void WorkerManager::Create(IUnitArbitrator^ arbitrator, IBaseManager^ baseManager)
		{
			UnitArbitrator^ lArbitrator = DirectCast<UnitArbitrator^>(arbitrator);
			BaseManager^ manager = DirectCast<BaseManager^>(baseManager);
			BWSAL::WorkerManager::destroy();
			s_instance = gcnew WorkerManager(BWSAL::WorkerManager::create(lArbitrator->instance, manager->instance));
		}
		
		WorkerManager^ WorkerManager::Instance::get()
		{
			return s_instance;
		}

		void WorkerManager::OnOffer(IEnumerable<Unit^>^ units)
		{
			instance->onOffer(ToSet<Unit^, BWAPI::Unit*>(units, &Api::ConvertUnit));
		}
		
		void WorkerManager::OnRevoke(Unit^ unit, double bid)
		{
			instance->onRevoke(ConvertUnit(unit), bid);
		}
		
		void WorkerManager::OnFrame()
		{
			instance->onFrame();
		}
		
		void WorkerManager::OnUnitComplete(Unit^ unit)
		{
			instance->onUnitComplete(ConvertUnit(unit));
		}
		
		String^ WorkerManager::Name::get()
		{
			return gcnew String(instance->getName().c_str());
		}

		void WorkerManager::SetWorkersPerGas(int count)
		{
			instance->setWorkersPerGas(count);
		}
		
		double WorkerManager::GetMineralRate()
		{
			return instance->getMineralRate();
		}
		
		double WorkerManager::GetGasRate()
		{
			return instance->getGasRate();
		}
		
		int WorkerManager::GetOptimalWorkerCount()
		{
			return instance->getOptimalWorkerCount();
		}
		
		int WorkerManager::MineralWorkerCount()
		{
			return instance->mineralWorkerCount();
		}
		
		int WorkerManager::GasWorkerCount()
		{
			return instance->gasWorkerCount();
		}
		
		void WorkerManager::EnableAutoBuild()
		{
			instance->enableAutoBuild();
		}
		
		void WorkerManager::DisableAutoBuild()
		{
			instance->disableAutoBuild();
		}
		
		void WorkerManager::SetAutoBuildPriority(int priority)
		{
			instance->setAutoBuildPriority(priority);
		}
	}
}


/*
#pragma once
#include <BWSAL/Arbitrator.h>
#include <BWAPI.h>
namespace BWSAL
{
  class Base;
  class BaseManager;
  class WorkerManager : public UnitController, public BWAPI::AIModule
  {
    public:
      static WorkerManager* create( UnitArbitrator* aribtrator, BaseManager* baseManager );
      static WorkerManager* getInstance();
      static void destroy();
      virtual void onOffer( std::set< BWAPI::Unit* > units );
      virtual void onRevoke( BWAPI::Unit* unit, double bid );
      virtual void onFrame();
      virtual void onUnitComplete( BWAPI::Unit* unit );
      virtual std::string getName() const;

      void setWorkersPerGas( int count );
      double getMineralRate() const;
      double getGasRate() const;
      int getOptimalWorkerCount() const;
      int mineralWorkerCount() const;
      int gasWorkerCount() const;
      void enableAutoBuild();
      void disableAutoBuild();
      void setAutoBuildPriority( int priority );

    private:
      class WorkerData
      {
        public:
          WorkerData() : m_resource( NULL ), m_newResource( NULL ), m_lastFrameSpam( NULL ) {}
          BWAPI::Unit* m_resource;
          BWAPI::Unit* m_newResource;
          int m_lastFrameSpam;
      };
      WorkerManager();
      ~WorkerManager();

      UnitArbitrator* m_arbitrator;
      BaseManager* m_baseManager;
      static WorkerManager* s_workerManager;

      void rebalanceWorkers();
      void updateWorkerAssignments();
      std::map< BWAPI::Unit*, WorkerData > m_workers;
      std::map< BWAPI::Unit*, std::set< BWAPI::Unit* > > m_currentWorkers;
      std::map< BWAPI::Unit*, Base* > m_resourceBase;
      std::map< BWAPI::Unit*, int > m_desiredWorkerCount;
      std::vector< std::pair< BWAPI::Unit*, int > > m_mineralOrder;
      int m_mineralOrderIndex;
      int m_lastSCVBalance;
      std::set< Base* > m_basesCache;
      int m_workersPerGas;
      double m_mineralRate;
      double m_gasRate;
      int m_mineralWorkers;
      int m_gasWorkers;
      bool m_autoBuild;
      int m_autoBuildPriority;
      int m_optimalWorkerCount;
  };
}
*/